﻿using ERPWinFormWork.Produce.Domain;
using ERPWinFormWork.Produce.ErrorCode;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static ERPWinFormWork.Produce.CreateProduction_Plan;

namespace ERPWinFormWork.Produce
{
    public partial class Dispatch_WorkForm : Form
    {
        private ApiClient _apiClient;

        public bool IsOperationCompleted { get; internal set; }
        public bool IsOperationCompleteds { get; internal set; }

        public Dispatch_WorkForm()
        {
            InitializeComponent();
            // 初始化 API 客户端（替换为你的实际 API 基础地址）
            _apiClient = new ApiClient(AppConfig.ApiBaseUrl);
            GetTeamSelect();
            LoadcboOther_MembersData();
            LoadcboQuality_InspectorsData();
            LoadcboQuality_DepartmentData();
        }


        private async void GetTeamSelect()
        {
            var response = await _apiClient.GetAsync<ApiResponse<List<TeamModel>>>(
                    endpoint: "Production_Plan/GetTeamModelHandle"
                );
            if (response != null)
            {
                cboTeam_Name.DataSource = response.pageData;
                cboTeam_Name.DisplayMember = "team_Name";//指定显示的字段
                cboTeam_Name.ValueMember = "team_Name";//绑定id属性
            }
        }
        private void cboTeam_Name_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedTeam_Name = cboTeam_Name.SelectedItem as TeamModel;
            if (selectedTeam_Name != null)
            {
                
                txtTask_Leader.Text = selectedTeam_Name.Task_Leader ?? "";
              
            }
            else
            {
                
                txtTask_Leader.Text = "";
                
            }
        }



        private void label1_Click(object sender, EventArgs e)
        {

        }

        public class CreateDispatch_Work
        {
            /// <summary>
            /// 班组名称
            /// </summary>
            public string Team_Name { get; set; }
            /// <summary>
            /// 班组负责人
            /// </summary>
            public string Task_Leader { get; set; }
            /// <summary>
            /// 其他成员
            /// </summary>
            public string Other_Members { get; set; }
            /// <summary>
            /// 备注（派工）
            /// </summary>
            public string Dispatch_Remarks { get; set; }
            /// <summary>
            /// 质检部门
            /// </summary>
            public string Quality_Department { get; set; }
            /// <summary>
            /// 质检人员
            /// </summary>
            public string Quality_Inspectors { get; set; }
            /// <summary>
            /// 备注（质检）
            /// </summary>
            public string Quality_Remarks { get; set; }
        }

        private async void btnsubmit_Click(object sender, EventArgs e)
        {
            // 构建请求实体对象，将窗体控件值赋值给实体属性
            var requestModel = new CreateDispatch_Work
            {
                Team_Name = cboTeam_Name.Text.Trim(),
                Task_Leader = txtTask_Leader.Text.Trim(),
                Other_Members = cboOther_Members.Text.Trim(),
                Dispatch_Remarks = txtDispatch_Remarks.Text.Trim(),
                Quality_Department = cboQuality_Department.Text.Trim(),
                Quality_Inspectors = cboQuality_Inspectors.Text.Trim(),
                Quality_Remarks = txtQuality_Remarks.Text.Trim(),
            };
            var response = await _apiClient.PostAsync<CreateDispatch_Work, ApiResponse<string>>(
                endpoint: "Production_Plan/CreateDispatch_WorkHandle",
                data: requestModel
            );
            if (response.code == 200)
            {
                // 这里可根据后端返回的结果做相应处理，比如简单提示成功
                MessageBox.Show("派工成功");
                // 可选择清空窗体控件内容，准备下次添加
                ClearControls();
                IsOperationCompleted = true;

                
                this.Close();
            }
            else
            {
                MessageBox.Show("派工失败");
            }

        }

        // 清空窗体控件内容的方法，按需使用
        private void ClearControls()
        {
            txtTask_Leader.Clear();
            txtDispatch_Remarks.Text.Clone();
            txtQuality_Remarks.Text.Clone();
            cboTeam_Name.SelectedIndex = -1;
            cboOther_Members.SelectedIndex = -1;
            cboQuality_Department.SelectedIndex = -1;
            cboQuality_Inspectors.SelectedIndex = -1;
            
        }
        private void LoadcboOther_MembersData()
        {
            // 模拟一个数据源，这里是简单的字符串列表，实际可从数据库等获取
            List<string> addressList = new List<string>()
            {
                "唐灵波",
                "池雅丽",
                "童芳洁",
                "昌新之",
                "禾和煦",
                "通湘云",
                "司空倚",
            };

            // 清空ComboBox原有项（可选，避免重复添加）
            cboOther_Members.Items.Clear();
            // 将数据源的数据添加到ComboBox中
            cboOther_Members.Items.AddRange(addressList.ToArray());

            // 可选：设置默认选中项，比如选第一个
            if (cboOther_Members.Items.Count > 0)
            {
                cboOther_Members.SelectedIndex = -1;
            }
        }

        private void LoadcboQuality_InspectorsData()
        {
            // 模拟一个数据源，这里是简单的字符串列表，实际可从数据库等获取
            List<string> addressList = new List<string>()
            {
                "唐灵波",
                "池雅丽",
                "童芳洁",
                "昌新之",
                "禾和煦",
                "通湘云",
                "司空倚",
            };

            // 清空ComboBox原有项（可选，避免重复添加）
            cboQuality_Inspectors.Items.Clear();
            // 将数据源的数据添加到ComboBox中
            cboQuality_Inspectors.Items.AddRange(addressList.ToArray());

            // 可选：设置默认选中项，比如选第一个
            if (cboQuality_Inspectors.Items.Count > 0)
            {
                cboQuality_Inspectors.SelectedIndex = -1;
            }
        }

        private void LoadcboQuality_DepartmentData()
        {
            // 模拟一个数据源，这里是简单的字符串列表，实际可从数据库等获取
            List<string> addressList = new List<string>()
            {
                "部门一",
                "部门二",
                "部门三",
               
            };

            // 清空ComboBox原有项（可选，避免重复添加）
            cboQuality_Department.Items.Clear();
            // 将数据源的数据添加到ComboBox中
            cboQuality_Department.Items.AddRange(addressList.ToArray());

            // 可选：设置默认选中项，比如选第一个
            if (cboQuality_Department.Items.Count > 0)
            {
                cboQuality_Department.SelectedIndex = -1;
            }
        }

        private void cboQuality_Department_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
